import 'package:flutter/material.dart';
import 'package:dio/dio.dart';

Dio dio = Dio();


class List extends StatefulWidget {
  List({Key key, @override this.mt}) : super(key: key);
  final String mt;
  @override
  _ListState createState() => _ListState();
}

class _ListState extends State<List> with AutomaticKeepAliveClientMixin {
  int page = 0;
  int total;
  var mlist = [];

  @override
  bool get wantKeepAlive => true;

  @override
  void initState() {
    super.initState();
    this.getmovielist();
  }

  Widget build(BuildContext context) {
    return ListView.builder(
      
      itemCount: mlist.length,
      itemBuilder: (BuildContext ctx, int i) {
        var items = mlist[i];
        return GestureDetector(
            onTap: () {
              Navigator.pushNamed(context, '/detail',
                  arguments: {'id': items['id'], 'title': items['title']});
            },
            child: Padding(
              padding: EdgeInsets.all(10.0),
              child: Container(
                decoration: BoxDecoration(
                    border: Border(top: BorderSide(color: Colors.black12))),
                child: Row(
                  children: <Widget>[
                    ClipRRect(
                      
                      borderRadius: BorderRadius.circular(5),
                      child: Image.network(
                        items['images']['small'],
                        height: 180,
                        width: 130,
                        fit: BoxFit.cover,
                      ),
                    ),
                    Container(
                      height: 200,
                      padding: EdgeInsets.only(left: 10),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        mainAxisAlignment: MainAxisAlignment.spaceAround,
                        children: <Widget>[
                          Container(
                            width: 300,
                            child: Text('电影名称: ${items['title']}',
                                overflow: TextOverflow.ellipsis,style: TextStyle(
                                          fontSize: 20, color: Colors.black)),
                          ),
                          Text('上映年份: ${items['mainland_pubdate']}年'),
                          Text('电影类型: ${items['genres'].join(',')}'),
                          Text('豆瓣评分: ${items['rating']['average']}分'),
                          Container(
                            child: Row(
                              children: <Widget>[
                                Padding(
                                  child: Text('演员列表:'),
                                  padding: EdgeInsets.fromLTRB(0, 0, 10, 0),
                                ),
                                ...this.flutts(i),
                              ],
                            ),
                          )
                        ],
                      ),
                    )
                  ],
                ),
              ),
            ));
      },
    );
  }

  getmovielist() async {
    var response = await dio.get(
        "https://douban.uieee.com/v2/movie/${widget.mt}?city=邯郸&count=10&start=${this.page * 10}");
    this.setState(() {
      total = response.data['total'];
      mlist = response.data['subjects'];
    });
  }

  flutts(x) {
    var ie;
    if (widget.mt == 'coming_soon') {
      ie = [
        Container(
          child: ClipOval(
              child: Image.asset(
            "images/a.jpg",
            width: 40,
            height: 40,
            fit: BoxFit.cover,
          )),
        )
      ];
    } else {
      ie = this.mlist[x]['casts'].map((item) {
        return Container(
          margin: EdgeInsets.only(left: 5),
          child: ClipOval(
              child: Image.network(
            item['avatars']['medium'],
            width: 40,
            height: 40,
            fit: BoxFit.cover,
          )),
        );
      });
    }

    return ie;
  }
}
